package com.hoshiibuy.organization.service.impl;

import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.organization.entity.PlatOrganization;
import com.hoshiibuy.organization.feign.PlatformService;
import com.hoshiibuy.organization.mapper.PlatOrganizationMapper;
import com.hoshiibuy.organization.service.PlatOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.organization.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Service
public class PlatOrganizationServiceImpl extends ServiceImpl<PlatOrganizationMapper, PlatOrganization> implements PlatOrganizationService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private PlatformService platformService;

    /**
    * @param id
    * @return com.hoshiibuy.organization.entity.PlatOrganization
    * @description: 根据id查询机构表
    */
    @Cacheable(cacheNames = "PlatOrganization", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "供应商信息-机构表 -查询机构表 ")
    public PlatOrganization getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增机构表
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganization", allEntries = true)
    @Logable(authName = "供应商信息-机构表-新增机构表")
    public boolean save(PlatOrganization entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        Response responseP = platformService.getById(entity.getPlatid());
        if(responseP.getCode() != 200 || responseP.getData() == null){
            throw new ServiceException(CommonError.error("新增失败,平台信息有误"));
        }
        Response responsePD = platformService.getById(entity.getPlatid());
        if(responsePD.getCode() != 200 || responsePD.getData() == null){
            throw new ServiceException(CommonError.error("新增失败,平台部门信息有误"));
        }

        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改机构表
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganization", allEntries = true)
    @Logable(authName = "供应商信息-机构表管理-修改机构表")
    public boolean updateById(PlatOrganization entity) {
        entity.setModifiedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
        throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询机构表
    */
    @Logable(authName = "供应商信息-机构表管理-分页查询机构表")
    @Cacheable(cacheNames = "PlatOrganization", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
    public IPage<PlatOrganization> page(IPage<PlatOrganization> page, Wrapper<PlatOrganization> queryWrapper) {
    return baseMapper.selectPage(page, queryWrapper);
    }
}
